package avicit.bdp.dgs.metadata.rest;

import avicit.bdp.dgs.metadata.dto.MetadataColumnTmpDTO;
import avicit.bdp.dgs.metadata.service.MetadataColumnTmpService;
import avicit.bdp.dgs.metadata.vo.MetadataColumnEditVo;
import avicit.platform6.core.rest.msg.QueryRespBean;
import avicit.platform6.core.rest.msg.ResponseMsg;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@Api(tags = "元数据列编辑")
@RequestMapping("/api/bdp/dgs/metadata/tmp/column")
public class MetadataColumnTmpRest {

  @Resource private MetadataColumnTmpService metadataColumnTmpService;

  @ApiOperation(value = "分页查询")
  @ApiImplicitParams({
    @ApiImplicitParam(
        value = "元数据ID",
        name = "metadataId",
        dataType = "String",
        paramType = "query"),
    @ApiImplicitParam(value = "查询关键词", name = "keyWords", dataType = "String", paramType = "query"),
    @ApiImplicitParam(value = "当前页", name = "pageNo", dataType = "Int", paramType = "query"),
    @ApiImplicitParam(value = "每页大小", name = "pageSize", dataType = "Int", paramType = "query")
  })
  @GetMapping
  public ResponseMsg<QueryRespBean<MetadataColumnTmpDTO>> getPageList(
      @RequestParam String metadataId,
      @RequestParam(required = false) String keyWords,
      @RequestParam(defaultValue = "1") Integer pageNo,
      @RequestParam(defaultValue = "10") Integer pageSize)
      throws Exception {

    ResponseMsg<QueryRespBean<MetadataColumnTmpDTO>> responseMsg = new ResponseMsg<>();
    QueryRespBean<MetadataColumnTmpDTO> respBean =
        metadataColumnTmpService.getPageList(metadataId, keyWords, pageNo, pageSize);
    responseMsg.setResponseBody(respBean);

    return responseMsg;
  }

  @PostMapping("/batchSaveOrUpdate")
  @ApiOperation(value = "批量修改或编辑")
  public ResponseMsg<Boolean> batchSaveOrUpdate(
      @RequestBody MetadataColumnEditVo metadataColumnEditVo) {
    ResponseMsg<Boolean> responseMsg = new ResponseMsg<>();
    responseMsg.setResponseBody(
        metadataColumnTmpService.batchSaveOrUpdate(metadataColumnEditVo) > 0);
    return responseMsg;
  }

  @ApiOperation(value = "根据ID删除")
  @ApiParam(value = "id", name = "id", required = true)
  @DeleteMapping("/{id}")
  public ResponseMsg<Boolean> batchDelete(@PathVariable String id) {
    ResponseMsg<Boolean> responseMsg = new ResponseMsg<>();
    responseMsg.setResponseBody(metadataColumnTmpService.deleteById(id) > 0);
    return responseMsg;
  }
}
